 //
//  AppDelegate.swift
//  仿写微博
//
//  Created by tuimao on 17/6/30.
//  Copyright © 2017年 tuimao. All rights reserved.
//

import UIKit
import UserNotifications
import SVProgressHUD
import AFNetworking
 
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        //额外信息设置
        setupAdditions()
        window = UIWindow()
        window?.rootViewController = WBMainViewController()
        window?.makeKeyAndVisible()
        loadAppInfo()
        return true
    }

}
 
//设置弹窗信息、授权
 extension AppDelegate{
    fileprivate func setupAdditions(){
        //设置SVProgress的显示最小时间
        SVProgressHUD.setMinimumDismissTimeInterval(1)
        //设置AFN的网络指示器
        AFNetworkActivityIndicatorManager.shared().isEnabled = true
        //取得用户授权的通知
        if #available(iOS 10.0, *) {
            UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { (issuccess, error) in
                
                print("授权 " + (issuccess ? "成功" : "失败"))
            }
        } else {
            let notifySettings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
            UIApplication.shared.registerUserNotificationSettings(notifySettings)
        }
    }
 }
 
 
extension AppDelegate{
    //模拟网络请求异步加载保存数据
    func loadAppInfo(){
        DispatchQueue.global().async {
            let url = Bundle.main.url(forResource: "main.json", withExtension: nil)
            let data = NSData(contentsOf: url!)
            let docDir = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
            let jsonpath = (docDir as NSString).appendingPathComponent("main.json")
            data?.write(toFile: jsonpath, atomically: true)
//            print("写入成功:\(jsonpath)")            
        }
        
    }
    
    
    
    
}
